#include <iostream>
using namespace std;
const int N=5e4+10;
int n,k;
int op,x,y;
int fa[N*3];
int find(int x)
{
	if(fa[x]==x)return x;
	return fa[x]=find(fa[x]);
}
void un(int x,int y)
{
	fa[find(x)]=find(y);
}
int main()
{
	cin>>n>>k;
	int ret=0;
	for(int i=1;i<=3*n;i++)fa[i]=i;
	while(k--)
	{
		cin>>op>>x>>y;
		if(x>n||y>n)
		{
		   ret++;
		   continue;
	   }
	   if(op==1)
	   {
	   	if(find(x+n)==find(y)||find(x+2*n)==find(y)){
	   		ret++;
	   		continue;
			}
			un(x,y);
			un(x+n,y+n);
			un(x+2*n,y+2*n);
		}
		else if(op==2)
		{
			if(find(x)==find(y)||find(x+2*n)==find(y)){
				ret++;
				continue;
			}
			if(x==y){
				ret++;
				continue;
			}
			un(x,y+2*n);
			un(x+n,y);
			un(x+2*n,y+n);
		}
	}
	cout<<ret;
	return 0;
}
